#include "key_task.h"
#include <etimer.h>
#include <process.h>
#include "device_manage.h"
#include "lib/list.h"
#include "wifi_task.h"

#include <stdlib.h>
#include <string.h>
#include "74hc595.h"
#include "adc.h"
#include "disp_task.h"
#include "fifo.h"
#include "fun_flash.h"
#include "lora_task.h"
#include "stm32f1xx_hal.h"
#include "time.h"
#include "uart.h"
#include "voice_task.h"

#define val_len 8  // ad采集个数
uint32_t ad_val[val_len];

uint8_t menu_index = 0;
uint8_t menu_status = f_start;
uint8_t menu_set = 2;

uint16_t add_num = 0;
uint16_t del_num = 0;
uint8_t is_delnum = 0;

extern uint8_t led;  //氛围灯

extern uint16_t disp_num_cur;

typedef struct {
  unsigned char count;       //计数
  unsigned char LongkeyFlag; /*按键长按标志，1表示长按*/
  unsigned char PreKeyValue; /*按键上一次值*/
} ScanKeyDef;

ScanKeyDef ScanKey_Def;
sEnterMenu entermenu;

BUTTON_T button[4];

static void KetVal_Init(void) {
  for (uint8_t i = 0; i < 4; i++) {
    button[i].KeyVal = Key_None + 1 + i;
    if (i > 1) {                       //按键：加和减支持连续按
      button[i].Type = Key_Cont_Type;  //按键有连续按功能
    } else {  //按键：设置/确认，清除/返回 不支持连续按
      button[i].Type = Key_Long_Type;  //按键支持长按功能
    }
  }
}

uint8_t Get_KeyVal(void) {
  static uint32_t temp;
  static uint8_t i = 0;

  for (i = 0; i < val_len; i++) {
    temp += ad_val[i];
  }
  temp /= val_len;

  if (3620 < temp && temp < 3800) {
    return Key_S;
  } else if (3380 < temp && temp < 3600) {
    return Key_A;
  } else if (2900 < temp && temp < 3350) {
    return Key_CR;
  } else if (2000 < temp && temp < 2700) {
    return Key_SC;
  } else {
    return Key_None;
  }
}

uint8_t KeyScan(void) {
  uint8_t KeyValue = Key_None;
  uint8_t KeyValueTemp = Key_None;
  static uint8_t KeyReleaseTimeCount = 0;

  if (cfg_data.Val.MacFlag != Mac_Init) {
    return Key_None;
  }

  KeyValueTemp = Get_KeyVal();

  if (KeyValueTemp != Key_None) {
    ScanKey_Def.count++;
    KeyReleaseTimeCount = 0;
    if (ScanKey_Def.count < LONG_PRESS_TIME) {
      ScanKey_Def.LongkeyFlag = 0;
      if (ScanKey_Def.count == SERIES_PRESS_TIME) {
        if (button[KeyValueTemp - 1].Type == Key_Cont_Type) {
          KeyValue = KeyValueTemp | Key_Mask_CONT;
          ScanKey_Def.PreKeyValue = KeyValue;
          ScanKey_Def.count = 0;
        }
      } else if (ScanKey_Def.count < SERIES_PRESS_TIME) {
        ScanKey_Def.PreKeyValue = KeyValueTemp;
      } else {
        ScanKey_Def.PreKeyValue = Key_None;
      }
    } else if (ScanKey_Def.count == LONG_PRESS_TIME) {
      if (button[KeyValueTemp - 1].Type == Key_Long_Type) {
        KeyValue = KeyValueTemp | Key_Mask_LONG;
        ScanKey_Def.LongkeyFlag = 1;
        //				ScanKey_Def.count = 0;
      }
      ScanKey_Def.PreKeyValue = Key_None;
    } else if (ScanKey_Def.count > LONG_PRESS_TIME) {
      ScanKey_Def.PreKeyValue = Key_None;
      KeyValue = Key_None;
      ScanKey_Def.count = LONG_PRESS_TIME;
    }
  } else {
    KeyReleaseTimeCount++;
    if (KeyReleaseTimeCount >= KEY_RELEASE_TIME_OUT) {
      KeyReleaseTimeCount = 0;
      if (ScanKey_Def.PreKeyValue != Key_None) {
        if (ScanKey_Def.LongkeyFlag == 0) {
          KeyValue = ScanKey_Def.PreKeyValue;
        }
      }
      ScanKey_Def.count = 0;
      ScanKey_Def.LongkeyFlag = 0;
      ScanKey_Def.PreKeyValue = Key_None;
    }
  }
  return KeyValue;
}

void adjust_setfun(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_up) {
    menu_index++;
    if (menu_index == SetMenu.count) {
      menu_index = menu_set;
    }
  } else if (flag == cmd_down) {
    menu_index--;
    if (menu_index < menu_set) {
      menu_index = SetMenu.count - 1;
    }
  } else {
  }

  if (SetMenu.menu[menu_index] == f_bell) {
    voice_value = voice_funbell;
  } else if (SetMenu.menu[menu_index] == f_remind) {
    voice_value = voice_funremind;
  } else if (SetMenu.menu[menu_index] == f_add) {
    voice_value = voice_funadd;
  } else if (SetMenu.menu[menu_index] == f_del) {
    voice_value = voice_fundel;
  } else if (SetMenu.menu[menu_index] == f_time) {
    voice_value = voice_funtime;
  } else if (SetMenu.menu[menu_index] == f_net) {
    voice_value = voice_funnet;
  }

  voice.cmd = voicecmd_playone;
  voice.value = (uint8_t*)&voice_value;
  process_start(&VoicePlay_Process, (process_data_t)&voice);

  set_data.cmd = disp_notwinkle;
  set_data.count = 12;
  set_data.timeout_handle = NULL;
  set_data.led[0] = NumDis[0x0F];
  set_data.led[1] = NumDis[menu_index - menu_set + 1];
  set_data.led[2] = 0x00;
  set_data.led[3] = 0x00;
  set_data.led[4] = 0x00;
  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);

  add_num = 0;
  Model = Model_Call;
}

void timeout_setfun() {
  entermenu.ctrl = ctrl_fun;
  adjust_setfun(cmd_enter);
  Model = Model_Call;
}

void adjust_voice(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t Volume;

  if (flag == cmd_up) {
    cfg_data.Val.Volume++;
    if (cfg_data.Val.Volume > volume_max) {
      cfg_data.Val.Volume = volume_min;
    }
    write_cfg(&cfg_data);  //保存设置音量
  } else if (flag == cmd_down) {
    cfg_data.Val.Volume--;
    if (cfg_data.Val.Volume < volume_min) {
      cfg_data.Val.Volume = volume_max;
    }
    write_cfg(&cfg_data);  //保存设置音量
  } else {
  }

  Volume = cfg_data.Val.Volume;
  voice.cmd = voicecmd_setvolume;
  voice.value = (uint8_t*)&Volume;
  process_start(&VoicePlay_Process, (process_data_t)&voice);

  set_data.cmd = disp_notwinkle;
  set_data.count = 2;
  set_data.timeout_handle = NULL;
  set_data.led[0] = 0x00;
  set_data.led[1] = 0x00;
  set_data.led[2] = 0x00;
  set_data.led[3] = NumDis[cfg_data.Val.Volume];
  set_data.led[4] = 0x00;
  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void adjust_bell(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t voice_value;
  static uint8_t BellIndex = 0;

  if (flag == cmd_up) {
    BellIndex++;
    if (BellIndex > bell_end) {
      BellIndex = bell_start;
    }
    set_data.count = 60;

    voice_value = BellIndex + voice_ringdingdong - 1;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  } else if (flag == cmd_down) {
    BellIndex--;
    if (BellIndex < bell_start) {
      BellIndex = bell_end;
    }
    set_data.count = 60;

    voice_value = BellIndex + voice_ringdingdong - 1;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  } else if (flag == cmd_enter) {
    BellIndex = cfg_data.Val.BellIndex;
    set_data.cmd = disp_twinkle;
    set_data.index = 3;
    set_data.count = 60;
    set_data.timeout_handle = NULL;

    voice_value = BellIndex + voice_ringdingdong - 1;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  } else if (flag == cmd_confirm) {
    cfg_data.Val.BellIndex = BellIndex;
    write_cfg(&cfg_data);  //保存设置铃声
    set_data.cmd = disp_notwinkle;
    set_data.count = 3;
    set_data.timeout_handle = timeout_setfun;
    led = Led_SetOk;
    process_start(&DispLed_Process, (process_data_t)&led);
    voice_value = voice_setok;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  }

  set_data.led[0] = 0x00;
  set_data.led[1] = 0x00;
  set_data.led[2] = 0x00;
  set_data.led[3] = NumDis[BellIndex];
  set_data.led[4] = 0x00;
  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void adjust_remind(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t voice_value;
  static uint8_t RemindCnt;

  if (flag == cmd_up) {
    RemindCnt++;
    if (RemindCnt > remind_max) {
      RemindCnt = remind_max;
    }
    set_data.count = 60;
  } else if (flag == cmd_down) {
    RemindCnt--;
    if (RemindCnt < remind_min) {
      RemindCnt = remind_min;
    }
    set_data.count = 60;
  } else if (flag == cmd_enter) {
    RemindCnt = cfg_data.Val.RemindCnt;
    set_data.cmd = disp_twinkle;
    set_data.index = 3;
    set_data.count = 60;
    set_data.timeout_handle = NULL;
  } else if (flag == cmd_confirm) {
    cfg_data.Val.RemindCnt = RemindCnt;
    write_cfg(&cfg_data);  //保存设置提醒
    set_data.cmd = disp_notwinkle;
    set_data.count = 3;
    set_data.timeout_handle = timeout_setfun;
    led = Led_SetOk;
    process_start(&DispLed_Process, (process_data_t)&led);
    voice_value = voice_setok;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  }

  set_data.led[0] = 0x00;
  set_data.led[1] = 0x00;
  set_data.led[2] = 0x00;
  set_data.led[3] = NumDis[RemindCnt];
  set_data.led[4] = 0x00;
  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void adjust_settime(uint8_t flag) {
  static sLEDData set_data;
  static TIMEDATE_TypeDef htimedate = {0};
  static uint8_t timeset_mod = 10;
  static uint8_t timeset_cnt = 10;
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_up) {
    timeset_cnt++;
    timeset_cnt = timeset_cnt % timeset_mod;
    set_data.led[set_data.index] = NumDis[timeset_cnt];

    if (set_data.index == 0) {
      htimedate.Hours = timeset_cnt * 10 + htimedate.Hours % 10 / 1;
    } else if (set_data.index == 1) {
      htimedate.Hours = timeset_cnt + htimedate.Hours / 10 * 10;
    } else if (set_data.index == 2) {
      htimedate.Minutes = timeset_cnt * 10 + htimedate.Minutes % 10 / 1;
    } else if (set_data.index == 3) {
      htimedate.Minutes = timeset_cnt + htimedate.Minutes / 10 * 10;
    }
    set_data.count = 60;
    //		printf("time:%d-%d-%d
    //%d:%d:%d\r\n",htimedate.Year,htimedate.Month,htimedate.Date,htimedate.Hours,htimedate.Minutes,htimedate.Seconds);
  } else if (flag == cmd_down) {
    timeset_cnt--;
    if (timeset_cnt == 0xff) {
      timeset_cnt = timeset_mod - 1;
    }
    timeset_cnt = timeset_cnt % timeset_mod;
    set_data.led[set_data.index] = NumDis[timeset_cnt];

    if (set_data.index == 0) {
      htimedate.Hours = timeset_cnt * 10 + htimedate.Hours % 10 / 1;
    } else if (set_data.index == 1) {
      htimedate.Hours = timeset_cnt + htimedate.Hours / 10 * 10;
    } else if (set_data.index == 2) {
      htimedate.Minutes = timeset_cnt * 10 + htimedate.Minutes % 10 / 1;
    } else if (set_data.index == 3) {
      htimedate.Minutes = timeset_cnt + htimedate.Minutes / 10 * 10;
    }
    set_data.count = 60;
    //		printf("time:%d-%d-%d
    //%d:%d:%d\r\n",htimedate.Year,htimedate.Month,htimedate.Date,htimedate.Hours,htimedate.Minutes,htimedate.Seconds);
  } else if (flag == cmd_enter) {
    set_data.cmd = disp_twinkle;
    set_data.index = 3;
    set_data.count = 60;
    set_data.timeout_handle = NULL;
    TimeDate_CountTime(&htimedate);
    set_data.led[0] = NumDis[htimedate.Hours % 100 / 10];
    set_data.led[1] = NumDis[htimedate.Hours % 10 / 1];
    set_data.led[2] = NumDis[htimedate.Minutes % 100 / 10];
    set_data.led[3] = NumDis[htimedate.Minutes % 10 / 1];
    set_data.led[4] = 0x00;

    timeset_mod = 10;
    timeset_cnt = htimedate.Minutes % 10 / 1;

    voice_value = voive_settime;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
    //		printf("time:%d-%d-%d
    //%d:%d:%d\r\n",htimedate.Year,htimedate.Month,htimedate.Date,htimedate.Hours,htimedate.Minutes,htimedate.Seconds);
  } else if (flag == cmd_switch) {
    if (set_data.index == 0) {
      set_data.index = 4;
    }
    set_data.index--;

    if (set_data.index == 3) {
      timeset_mod = 10;
      timeset_cnt = htimedate.Minutes % 10 / 1;
    } else if (set_data.index == 2) {
      timeset_mod = 6;
      timeset_cnt = htimedate.Minutes % 100 / 10;
    } else if (set_data.index == 1) {
      if ((htimedate.Hours % 100 / 10) == 2) {
        timeset_mod = 4;
      } else {
        timeset_mod = 10;
      }
      timeset_cnt = htimedate.Hours % 10 / 1;
    } else if (set_data.index == 0) {
      if ((htimedate.Hours % 10 / 1) > 3) {
        timeset_mod = 2;
      } else {
        timeset_mod = 3;
      }
      timeset_cnt = htimedate.Hours % 100 / 10;
    }
    set_data.count = 60;
  } else if (flag == cmd_longconf) {
    TimeDate_SetTime(&htimedate);
    set_data.cmd = disp_notwinkle;
    set_data.count = 3;
    set_data.timeout_handle = timeout_setfun;
    led = Led_SetOk;
    process_start(&DispLed_Process, (process_data_t)&led);
    voice_value = voice_setok;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  }

  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void adjust_addnum(uint8_t flag) {
  static sLEDData set_data;
  static uint8_t ge[1];
  static uint8_t shi[1];
  static uint8_t bai[1];
  static uint8_t* dat = NULL;
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_up) {
    (*dat)++;
    if ((*dat) == 10) {
      (*dat) = 0;
    }
    add_num = bai[0] * 100 + shi[0] * 10 + ge[0];
    set_data.count = 60;
  } else if (flag == cmd_down) {
    if ((*dat) == 0) {
      (*dat) = 10;
    }
    (*dat)--;
    add_num = bai[0] * 100 + shi[0] * 10 + ge[0];
    set_data.count = 60;
  } else if (flag == cmd_enter) {
    bai[0] = add_num % 1000 / 100;
    shi[0] = add_num % 100 / 10;
    ge[0] = add_num % 10 / 1;

    if (add_num == 0) {
      voice_value = voice_addnum;
      voice.cmd = voicecmd_playone;
      voice.value = (uint8_t*)&voice_value;
      process_start(&VoicePlay_Process, (process_data_t)&voice);
    }

    set_data.cmd = disp_twinkle;
    set_data.count = 60;
    set_data.index = 3;
    dat = ge;

    Model = Model_Add;
  } else if (flag == cmd_switch) {
    if (set_data.index == 1) {
      set_data.index = 4;
    }
    set_data.cmd = disp_twinkle;
    set_data.count = 60;
    set_data.index--;
    if (set_data.index == 3) {
      dat = ge;
    } else if (set_data.index == 2) {
      dat = shi;
    } else if (set_data.index == 1) {
      dat = bai;
    }
  }

  set_data.led[0] = 0x00;
  set_data.led[1] = NumDis[*bai];
  set_data.led[2] = NumDis[*shi];
  set_data.led[3] = NumDis[*ge];
  set_data.led[4] = 0x00;

  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void adjust_delnum(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_enter) {
    set_data.cmd = disp_notwinkle;
    set_data.count = 60;
    set_data.index = 0;
    set_data.led[0] = 0x00;
    set_data.led[1] = NumDis[0x10];
    set_data.led[2] = NumDis[0x10];
    set_data.led[3] = NumDis[0x10];
    set_data.led[4] = 0x00;
    process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
    Model = Model_Del;

    voice_value = voice_pressdell;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  } else if (flag == cmd_longconf) {
    if (is_delnum == 1) {
      del_flashnum();
      set_data.cmd = disp_delay;
      set_data.count = 60;
      set_data.index = 0;
      set_data.led[0] = 0x00;
      set_data.led[1] = NumDis[0x10];
      set_data.led[2] = NumDis[0x10];
      set_data.led[3] = NumDis[0x10];
      set_data.led[4] = 0x00;
      process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
      led = Led_SetOk;
      process_start(&DispLed_Process, (process_data_t)&led);
      set_delnum(0);

      voice_value = voice_delsuccess;
      voice.cmd = voicecmd_playone;
      voice.value = (uint8_t*)&voice_value;
      process_start(&VoicePlay_Process, (process_data_t)&voice);
    } else {
      entermenu.ctrl = ctrl_delallnum;
      led = Led_SetOk;
      process_start(&DispLed_Process, (process_data_t)&led);

      voice_value = voice_longpressagain;
      voice.cmd = voicecmd_playone;
      voice.value = (uint8_t*)&voice_value;
      process_start(&VoicePlay_Process, (process_data_t)&voice);
    }
  }
}

void adjust_delallnum(uint8_t flag) {
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_longconf) {
    del_allflashnum();
    entermenu.ctrl = ctrl_fun;
    adjust_setfun(cmd_enter);
    led = Led_SetOk;
    process_start(&DispLed_Process, (process_data_t)&led);

    voice_value = voice_delallsuccess;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  }
}

void adjust_setnet(uint8_t flag) {
  static sLEDData set_data;
  static sVoiceCmd voice;
  static uint8_t voice_value;

  if (flag == cmd_enter) {
    set_data.cmd = disp_notwinkle;
    set_data.count = 120;
    set_data.timeout_handle = NULL;
    set_data.led[0] = NumDis[0x0E];
    set_data.led[1] = NumDis[menu_index - menu_set + 1];
    set_data.led[2] = 0x00;
    set_data.led[3] = 0x00;
    set_data.led[4] = 0x00;
    process_start(&Wifi_SetNet_Process, NULL);  //开始配网任务
    process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
    led = Led_SetNet;
    process_start(&DispLed_Process, (process_data_t)&led);

    voice_value = voice_openwechat;
    voice.cmd = voicecmd_playone;
    voice.value = (uint8_t*)&voice_value;
    process_start(&VoicePlay_Process, (process_data_t)&voice);
  } else if (flag == cmd_back) {
    process_start(&Wifi_UnSetNet_Process, NULL);  //退出配网任务
    led = Led_None;
    process_start(&DispLed_Process, (process_data_t)&led);
  }
}

void adjust_num(uint8_t flag) {
  static sLEDData set_data;

  if (flag == cmd_back) {
    remove_dispnum(disp_num_cur);
	remove_callnum(disp_num_cur);
    set_data.cmd = disp_num;
    set_data.count = 1;
  } else if (flag == cmd_longback) {
    remove_callnum_all();
    remove_dispnum_all();
    set_data.cmd = disp_num;
    set_data.count = 1;
    led = Led_DevInit;
    process_start(&DispLed_Process, (process_data_t)&led);
  }
  process_post(&Disp_Process, PROCESS_EVENT_MSG, (process_data_t)&set_data);
}

void enter_dispnum() { entermenu.ctrl = ctrl_num; }

uint8_t get_menuctrl() { return entermenu.ctrl; }

void set_delnum(uint8_t isdel) { is_delnum = isdel; }

void set_handle() {
  static sLEDData set_data;
  if ((entermenu.ctrl > ctrl_none) && (entermenu.cmd > cmd_none)) {
    if (entermenu.ctrl == ctrl_voice) {  //声音调节
      menu_index =
          (&SetMenu.menu[f_voice]) - (&SetMenu.menu[f_start]);  //更改菜单下标
      if (entermenu.cmd == cmd_up) {
        adjust_voice(cmd_up);  //增加声音
      } else if (entermenu.cmd == cmd_down) {
        adjust_voice(cmd_down);  //减少声音
      }
      entermenu.ctrl = ctrl_none;
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_fun) {  //功能选择
      if (entermenu.cmd == cmd_enter) {
        menu_index = menu_set - 1;
        adjust_setfun(cmd_up);
        entermenu.fun = SetMenu.menu[menu_index];
      } else if (entermenu.cmd == cmd_back) {
        entermenu.fun = f_start;
        entermenu.ctrl = ctrl_none;
        set_data.cmd = disp_cmdfinish;
        set_data.count = 0;
        process_post(&Disp_Process, PROCESS_EVENT_MSG,
                     (process_data_t)&set_data);
      } else if (entermenu.cmd == cmd_up) {
        adjust_setfun(cmd_up);
        entermenu.fun = SetMenu.menu[menu_index];
      } else if (entermenu.cmd == cmd_down) {
        adjust_setfun(cmd_down);
        entermenu.fun = SetMenu.menu[menu_index];
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_setbell) {  //铃声设置
      if (entermenu.cmd == cmd_enter) {
        adjust_bell(cmd_enter);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
      } else if (entermenu.cmd == cmd_up) {
        adjust_bell(cmd_up);
      } else if (entermenu.cmd == cmd_down) {
        adjust_bell(cmd_down);
      } else if (entermenu.cmd == cmd_confirm) {
        adjust_bell(cmd_confirm);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_setremind) {  //提醒设置
      if (entermenu.cmd == cmd_enter) {
        adjust_remind(cmd_enter);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
      } else if (entermenu.cmd == cmd_up) {
        adjust_remind(cmd_up);
      } else if (entermenu.cmd == cmd_down) {
        adjust_remind(cmd_down);
      } else if (entermenu.cmd == cmd_confirm) {
        adjust_remind(cmd_confirm);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_add) {  //添加分机
      if (entermenu.cmd == cmd_enter) {
        adjust_addnum(cmd_enter);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
      } else if (entermenu.cmd == cmd_up) {
        adjust_addnum(cmd_up);
      } else if (entermenu.cmd == cmd_down) {
        adjust_addnum(cmd_down);
      } else if (entermenu.cmd == cmd_switch) {
        adjust_addnum(cmd_switch);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_del) {  //删除分机
      if (entermenu.cmd == cmd_enter) {
        adjust_delnum(cmd_enter);
        set_delnum(0);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
      } else if (entermenu.cmd == cmd_longconf) {
        adjust_delnum(cmd_longconf);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_delallnum) {  //删除全部分机
      if (entermenu.cmd == cmd_longconf) {
        adjust_delallnum(cmd_longconf);
      }
    } else if (entermenu.ctrl == ctrl_time) {  //时间设置
      if (entermenu.cmd == cmd_enter) {
        adjust_settime(cmd_enter);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
      } else if (entermenu.cmd == cmd_up) {
        adjust_settime(cmd_up);
      } else if (entermenu.cmd == cmd_down) {
        adjust_settime(cmd_down);
      } else if (entermenu.cmd == cmd_switch) {
        adjust_settime(cmd_switch);
      } else if (entermenu.cmd == cmd_longconf) {
        adjust_settime(cmd_longconf);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_net) {  //网络设置
      if (entermenu.cmd == cmd_enter) {
        adjust_setnet(cmd_enter);
      } else if (entermenu.cmd == cmd_back) {
        entermenu.ctrl = ctrl_fun;
        adjust_setfun(cmd_enter);
        adjust_setnet(cmd_back);
      }
      entermenu.cmd = cmd_none;
    } else if (entermenu.ctrl == ctrl_num) {  //呼叫号码
      if (entermenu.cmd == cmd_back) {
        adjust_num(cmd_back);
      } else if (entermenu.cmd == cmd_longback) {
        adjust_num(cmd_longback);
      }
      entermenu.cmd = cmd_none;
    }
  }
}

void setnet_finish(void)  //配置网络完成，回到功能界面
{
  entermenu.cmd = cmd_back;
  set_handle();
}

uint16_t get_addnum(void) { return add_num; }

void enter_addnum(void) {
  entermenu.cmd = cmd_enter;
  set_handle();
}

PROCESS(Key_Process, "Key");
PROCESS_THREAD(Key_Process, ev, data) {
  static struct etimer et;
  static uint8_t key_val;

  PROCESS_BEGIN();

  KetVal_Init();                                           //按键初始化
  HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ad_val, val_len);  //按键开始ADC检测

  menu_index = 0;
  entermenu.ctrl = ctrl_none;
  entermenu.cmd = cmd_none;
  entermenu.fun = f_start;

  etimer_set(&et, 500);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

  Model = Model_Call;

  while (1) {
    key_val = KeyScan();

    switch (key_val) {
      case Key_A: {                         //上翻
                                            //				uart_send((uint8_t *)"Key_A",
                                            // strlen("Key_A"));
        if (entermenu.ctrl == ctrl_none) {  //没有操作
          entermenu.ctrl = ctrl_voice;      //音量调节操作
          entermenu.cmd = cmd_up;           //音量减
        } else if (entermenu.ctrl >= ctrl_fun &&
                   entermenu.ctrl < ctrl_net) {  //设置功能操作
          entermenu.cmd = cmd_up;                //上翻
        }
        break;
      }
      case Key_S: {                         //下翻
                                            //				uart_send((uint8_t *)"Key_S",
                                            // strlen("Key_S"));
        if (entermenu.ctrl == ctrl_none) {  //没有操作
          entermenu.ctrl = ctrl_voice;      //音量调节操作
          entermenu.cmd = cmd_down;         //音量减
        } else if (entermenu.ctrl >= ctrl_fun &&
                   entermenu.ctrl < ctrl_net) {  //设置功能操作
          entermenu.cmd = cmd_down;              //下翻
        }
        break;
      }
      case Key_SC: {                       //确定
                                           //				uart_send((uint8_t
                                           //*)"Key_SC",  strlen("Key_SC"));
        if (entermenu.ctrl == ctrl_fun) {  //功能菜单中
          if (entermenu.fun == f_bell) {   //选定 铃声设置
            entermenu.ctrl = ctrl_setbell;
          } else if (entermenu.fun == f_remind) {  //选定 提醒次数设置
            entermenu.ctrl = ctrl_setremind;
          } else if (entermenu.fun == f_add) {  //选定 添加分机
            entermenu.ctrl = ctrl_add;
          } else if (entermenu.fun == f_del) {  //选定 删除分机
            entermenu.ctrl = ctrl_del;
          } else if (entermenu.fun == f_time) {  //选定 时间设置
            entermenu.ctrl = ctrl_time;
          } else if (entermenu.fun == f_net) {  //选定 网络设置
            entermenu.ctrl = ctrl_net;
          }
          entermenu.cmd = cmd_enter;  //进入设置界面
        } else if (entermenu.ctrl ==
                   ctrl_time) {  //设置时间界面，按确认切换操作的位
          entermenu.cmd = cmd_switch;
        } else if (entermenu.ctrl ==
                   ctrl_add) {  //添加分家界面，按确认切换操作的位
          entermenu.cmd = cmd_switch;
        } else if (entermenu.ctrl == ctrl_setbell) {  //保存设置的铃声
          entermenu.cmd = cmd_confirm;
        } else if (entermenu.ctrl == ctrl_setremind) {  //保存设置的提醒
          entermenu.cmd = cmd_confirm;
        }
        break;
      }
      case Key_CR: {  //返回
                      //				uart_send((uint8_t
                      //*)"Key_CR",  strlen("Key_CR"));
        if (entermenu.ctrl >= ctrl_fun) {
          entermenu.cmd = cmd_back;
        }
        break;
      }
      case Key_A | Key_Mask_CONT: {  //连续上翻
        //				uart_send((uint8_t
        //*)"Key_A|Key_Mask_CONT",  strlen("Key_A|Key_Mask_CONT"));
        if (SetMenu.menu[menu_index] == f_start ||
            SetMenu.menu[menu_index] == f_voice) {
          menu_index = (&SetMenu.menu[f_voice]) - (&SetMenu.menu[f_start]);
          adjust_voice(cmd_up);  //增加声音
        }
        break;
      }
      case Key_S | Key_Mask_CONT: {  //连续下翻
        //				uart_send((uint8_t
        //*)"Key_S|Key_Mask_CONT",  strlen("Key_S|Key_Mask_CONT"));
        if (SetMenu.menu[menu_index] == f_start ||
            SetMenu.menu[menu_index] == f_voice) {
          menu_index = (&SetMenu.menu[f_voice]) - (&SetMenu.menu[f_start]);
          adjust_voice(cmd_down);  //减少声音
        }
        break;
      }
      case Key_SC | Key_Mask_LONG: {  //设置
        //				uart_send((uint8_t
        //*)"Key_SC|Key_Mask_LONG",  strlen("Key_SC|Key_Mask_LONG"));
        if (entermenu.ctrl == ctrl_none) {
          entermenu.ctrl = ctrl_fun;
          entermenu.cmd = cmd_enter;
        } else if (entermenu.ctrl == ctrl_time) {  //保存设置时间
          entermenu.cmd = cmd_longconf;
        } else if (entermenu.ctrl == ctrl_del) {  //删除分机
          entermenu.cmd = cmd_longconf;
        } else if (entermenu.ctrl == ctrl_delallnum) {  //删除全部分机
          entermenu.cmd = cmd_longconf;
        }
        break;
      }
      case Key_CR | Key_Mask_LONG: {  //清除
        //				uart_send((uint8_t
        //*)"Key_CR|Key_Mask_LONG",  strlen("Key_CR|Key_Mask_LONG"));
        if (entermenu.ctrl == ctrl_num) {
          entermenu.cmd = cmd_longback;
        }
        break;
      }
    }

    set_handle();  //设置操作处理

    Feed_KeyDog();

    etimer_set(&et, 100);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
  }

  PROCESS_END();
}
